#version 450 compatibility




layout(local_size_x = DEF_LocalSize, local_size_y = DEF_LocalSize, local_size_z = 1) in;

layout( binding = 0, rgba8ui) uniform uimage2D Pix;

layout( binding = 1 ) uniform Buf_LoalParameter1 {
    ivec4   m_Rect;
    ivec2   m_WidgetOffset;
    uint    m_Color;
    uint    m_OverlayMode;
};



void main(){
    ivec2 pos = ivec2(gl_WorkGroupID.xy);

    if(m_Rect.x>0) pos.x += m_Rect.x;
	if(m_Rect.y>0) pos.y += m_Rect.y;
    
    
    //if(m_WidgetOffset.x<0) pos.x += m_WidgetOffset.x;
	//if(m_WidgetOffset.y<0) pos.y += m_WidgetOffset.y;

    if (pos.x < m_Rect.z && pos.y < m_Rect.w) {
    //if (pos.x >= m_Rect.x && pos.y >= m_Rect.y && pos.x < m_Rect.z && pos.y < m_Rect.w) {
        //pos.x += m_Rect.x;
	    //pos.y += m_Rect.y;


        uvec4 c = uvec4(bitfieldExtract(m_Color, 0, 8), 
                    bitfieldExtract(m_Color, 8, 8), 
                    bitfieldExtract(m_Color, 16, 8), 
                    bitfieldExtract(m_Color, 24, 8));
        //c = uvec4(25,255,25, 255);
        //uvec4 color = imageLoad(Pix, pos);

        //float alp = (float(c.a) / 255.0);
        //float i_alp = 1.0 - alp;

        //if (m_WidgetOffset.x+gl_NumWorkGroups.x >= m_Rect.z && m_WidgetOffset.y+gl_NumWorkGroups.y >= m_Rect.w) {
        //if (pos.x >= m_Rect.z && pos.y >= m_Rect.w) {
        //    c.a = 0;
        //}
        /*switch(m_OverlayMode){
        case 1:
            c.r = uint(float(color.r)*i_alp + float(c.r)*alp);
            c.g = uint(float(color.g)*i_alp + float(c.g)*alp);
            c.b = uint(float(color.b)*i_alp + float(c.b)*alp);
            break;
        case 5:
            c.rgb = uvec3(255,255,255) - color.rgb;
            break;
        }*/
        //c.rgb = uvec3(m_OverlayMode*20,m_OverlayMode*20,m_OverlayMode*20);
        
        imageStore(Pix, pos, c);
        
    }
    //if (pos.x >= m_Rect.z || pos.y >= m_Rect.w) return;
    //if (pos.x >= gl_NumWorkGroups.x || pos.y >= gl_NumWorkGroups.y) return;
	
    
    


    

    
}


